package com.swpu.cbg.cnshop.adapter;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.bean.ShoppingCart;
import com.swpu.cbg.cnshop.utils.CartProvider;
import com.swpu.cbg.cnshop.widget.NumAddSubView;

import java.util.Iterator;
import java.util.List;

import javax.xml.validation.Validator;

/**
 * Created by chenboge on 16/2/12.
 */
public class CartAdapter<T> extends BaseAdapter<T, BaseViewHolder> implements View.OnClickListener {


    private CheckBox mCheckBox;
    private TextView mTotleText;
    private CartProvider mProvider;

    public CartAdapter(Context mContext, List<T> mDatas, int itemViewID, CheckBox checkBox, TextView textView) {
        super(mContext, mDatas, itemViewID);
        this.mCheckBox = checkBox;
        this.mTotleText = textView;
        mCheckBox.setOnClickListener(this);
        mProvider = new CartProvider(mContext);
    }

    @Override
    void bindData(BaseViewHolder holder, int position) {
        final ShoppingCart cart = (ShoppingCart) mDatas.get(position);
        holder.getTextView(R.id.id_cart_descripts).setText(((ShoppingCart) mDatas.get(position)).getName());
        holder.getTextView(R.id.id_cart_price).setText("￥" + ((ShoppingCart) mDatas.get(position)).getPrice() + "");
        Picasso.with(mContext).load(((ShoppingCart) mDatas.get(position)).getImgUrl()).into(holder.getImageView(R.id.id_cart_image));
        CheckBox checkBox = (CheckBox) holder.getView(R.id.id_cart_checkbox);
        checkBox.setChecked(cart.isChecked());
        NumAddSubView view = (NumAddSubView) holder.getView(R.id.id_cart_addSubView);
        view.setValue(((ShoppingCart) mDatas.get(position)).getCount());
        view.setOnAddSubClickListener(new NumAddSubView.OnAddSubClickListener() {

            @Override
            public void AddNum(View view, int value) {
                cart.setCount(value);
                mProvider.update(cart);
                mDatas = (List<T>) mProvider.getAll();
                showTotleText();
            }

            @Override
            public void SubNum(View view, int value) {
                cart.setCount(value);
                mProvider.update(cart);
                mDatas = (List<T>) mProvider.getAll();
                showTotleText();
            }
        });
        setOnItemClickListener(new onItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                ShoppingCart cart1 = (ShoppingCart) mDatas.get(position);
                cart1.setIsChecked(!cart1.isChecked());
                notifyItemChanged(position);
                checkListen();
                showTotleText();
            }
        });
    }


    public void showTotleText() {
        mTotleText.setText("合计：" + getTotlePrice() + "元");
    }

    private float getTotlePrice() {
        float sum = 0;
        if (!isNull()) {
            return sum;
        } else {
            for (T cart : mDatas) {
                ShoppingCart cart1 = (ShoppingCart) cart;
                if (cart1.isChecked()) {
                    sum += cart1.getCount() * cart1.getPrice();
                }
            }
        }
        return sum;
    }

    private boolean isNull() {
        return (mDatas != null && mDatas.size() > 0);
    }

    private void checkListen() {
        int count = 0;
        int checkNum = 0;
        if (mDatas != null) {
            count = mDatas.size();
            for (T cart : mDatas) {
                ShoppingCart cart1 = (ShoppingCart) cart;
                if (!cart1.isChecked()) {
                    mCheckBox.setChecked(false);
                    break;
                } else {
                    checkNum++;
                }
            }
            if (count == checkNum) {
                mCheckBox.setChecked(true);
            }
        }
    }

    @Override
    public void onClick(View v) {
        checkAll_None(mCheckBox.isChecked());
       // mProvider.SaveToLocal();
        showTotleText();
    }

    public void checkAll_None(boolean chack) {
        if (!isNull()) {
            return;
        }
        int i = 0;
        for (T cart : mDatas) {
            ShoppingCart cart1 = (ShoppingCart) cart;
            cart1.setIsChecked(chack);
            notifyItemChanged(i);
            i++;
        }
    }


//    public void delCart(){
//        if(!isNull()){
//            return;
//        }
//
//        for(T cart:mDatas) {
//            ShoppingCart cart1 = (ShoppingCart) cart;
//            if(cart1.isChecked()) {
//                int position = mDatas.indexOf(cart1);
//                mProvider.delete(cart1);
//                mDatas.remove(cart1);
//                notifyItemRemoved(position);
//            }
//        }
//    }


    public void delCart(){
        if(!isNull()){
            return;
        }

      for(Iterator iterator=mDatas.iterator();iterator.hasNext();) {
          ShoppingCart cart = (ShoppingCart) iterator.next();
          if(cart.isChecked()) {
                int position = mDatas.indexOf(cart);
                mProvider.delete(cart);
                iterator.remove();
                notifyItemRemoved(position);
            }
      }
    }
}
